﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AiXiu.Model;

namespace Aixiu.DAL
{
    public class VideoDAL
    {
        AiXiuDbContent dbContent = new AiXiuDbContent();

        /// <summary>
        /// 上传视频
        /// </summary>
        /// <param name="video"></param>
        /// <returns></returns>
        public TBVideos AddVideo(TBVideos video)
        {
            video.UploadTime = DateTime.Now;
            dbContent.Entry(video).State = EntityState.Added;
            dbContent.SaveChanges();
            return video;
        }
        /// <summary>
        /// 获取处理中的视频ID列表（上传中、上传成功、转码中)
        /// </summary>
        /// <returns></returns>
        public List<string> GetInProcessVideoIds()
        {
            //获取处理中的视频ID列表（上传中、上传成功、转码中)
            List<TBVideos> list = dbContent.TBVideos.Where(a => a.Status == (int)VideoStatus.Uploading || a.Status == (int)VideoStatus.UploadSucces || a.Status == (int)VideoStatus.Transcoding||a.Status==(int)VideoStatus.Normal).ToList();
            //foreach循环
            //List<string> idList = new List<string>();
            //foreach(var item in list)
            //{
            //    idList.Add(item.VideoId);
            //}
            //return idList;

            List<string> videoIds = list.Select(a => a.VideoId).ToList();
            return videoIds;
        }

        /// <summary>
        /// 批量更新视频信息
        /// </summary>
        /// <param name="videos">待更新的视频信息</param>
        public void UpdateVideos(List<TBVideos> videos)
        {
            foreach (TBVideos v in videos)
            {
                TBVideos videoEtity = dbContent.TBVideos.Find(v.VideoId);
                if (!string.IsNullOrEmpty(v.CoverURL))
                {
                    videoEtity.CoverURL = v.CoverURL.Substring(0, v.CoverURL.IndexOf('?'));
                }

                videoEtity.Status = v.Status;
                dbContent.Entry(videoEtity).State = EntityState.Modified;
                dbContent.SaveChanges();
            }
        }
        /// <summary>
        /// 获取视频列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<TBVideos> GetVideos()
        {
            int status = (int)VideoStatus.Normal;
            return dbContent.TBVideos
                .Where(m => m.Status == status)
                .Include(m => m.TBUsers)    // 连接查询用户信息表
                .OrderByDescending(m => m.UploadTime)
                .ToList();
        }
        /// <summary>
        /// 通过视频Id获取视频信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public TBVideos GetVideoByVideoId(string videoId)
        {
            TBVideos video = dbContent.TBVideos
                 .Include(m => m.TBUsers)
                 .SingleOrDefault(m => m.VideoId == videoId);
            return video;
        }
        /// <summary>
        /// 根据用户id获取视频列表
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public List<TBVideos> GetVideosByUserId(int userId)
        {
            return dbContent.TBVideos
                .Where(m => m.UserId == userId)
                .OrderByDescending(m => m.UploadTime)
                .ToList();
        }
        /// <summary>
        /// 删除视频
        /// </summary>
        /// <param name="videoId">视频Id</param>
        public int DeleteVideo(string videoId)
        {
            // 从数据库删除
            TBVideos videoEntity = dbContent.TBVideos.Find(videoId);
            dbContent.Entry(videoEntity).State = EntityState.Deleted;
            int res = dbContent.SaveChanges();
            return res;
        }
    }
}
